<?php
$isReloadable = true;
require_once("../../pers/inc.config.php");
require_once(R2_FS_WEB_ROOT.'/app/_inc/inc.session.php');
require_once(R2_FS_WEB_ROOT.'/app/_inc/funct.html.php');

$aWhere = array();
if (reqVar('pagifilt')!='') {
	$aWhere[] = " (c.descr LIKE '%".addslashes(reqVar('pagifilt'))."%' OR c.cod_comm LIKE '%".addslashes(reqVar('pagifilt'))."%') ";
}
$sWhere = (count($aWhere)==0)? "":" WHERE ".implode(" AND ",$aWhere);

$sql_sel = "SELECT count(*) AS num_rec FROM commessa c " .$sWhere;
$objDB->query($sql_sel,'selRec');
$acRec = $objDB->fetch('selRec');
$numRec = $acRec['num_rec'];

$link = $_SERVER['PHP_SELF']."?".$sXSNQS;

$startOffset = 0;
$endOffset   = 0;
$sFiltro  = rint2GetFiltro($link);
$sPaginaz = rint2GetPaginaz($link, $numRec, $startOffset, $endOffset);

$sql_sel = " SELECT c.* FROM commessa c ".$sWhere
	." ORDER BY IF(c.data_fine < '".date('Y-m-d')."',1,0), c.id_comm LIMIT "
	.$startOffset.",".($endOffset-$startOffset);
$objDB->query($sql_sel,'selRec');
//dbg_($sql_sel);

// Clienti commessa.
$sSqlCli = "SELECT * FROM clienti_comm";
$objDB->query($sSqlCli,'selCli');
$aCli = array();
while ($acCli = $objDB->fetch('selCli')) {	
	$aCli[$acCli['id_comm']][] = $aClientiRint[$acCli['id_cliente']]['nome'];	
}

$cmdL = "";
$cmdL .= "<a class=\"cmd1\" href=\"cliList.php?$sXSNQS\">clienti</a>";
$cmdL .= "&nbsp;&nbsp;&nbsp;";

$cmdR = "";
if ($objSess->abilitaz['gescomm'] == 'W') {
	$cmdR .= "&nbsp;&nbsp;&nbsp;";
	$cmdR .= "<a class=\"cmd1\" href=\"commIns.php?$sXSNQS\">nuova commessa</a>";
}
$cmdR .= cmdTorna(); 

$aMenuPag = array();
include("./_inc/inc.aMenuPag.php");
testaHtml('Lista Commesse', $msgPag, '30.10.10', $aMenuPag);

echo getHtmlForm();
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('startoffset');
?>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left"><?=$cmdL?></td>
      <td class="cmd1" align="right"><?=$cmdR?></td>
   </tr>
   <tr>
      <td class="cmd1" align="left"><?=$sFiltro?></td>
      <td class="cmd1" align="right"><?=$sPaginaz?></td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="tit1" colspan='9' align='center'>Lista commesse</td>
   </tr>
   <tr>
      <td class="lab1" width='' align='center'>ID</td>
      <td class="lab1" width='' align='center'>Cod.</td>
      <td class="lab1" width='' align='center'>Descrizione</td>
      <td class="lab1" width='' align='center'>Co.Ge.</td>
      <td class="lab1" width='' align='center'>Data Inizio</td>
      <td class="lab1" width='' align='center'>Data Fine</td>
      <td class="lab1" width='' align='center'>Responsabile</td>
      <td class="lab1" width='' align='center'>Cliente no-rint</td>      
      <td class="lab1" width='' align='center'>Clienti rint</td>
   </tr>
<? 
$clRF = 'txt2';
while ($acComm = $objDB->fetch('selRec')) { 
	$clRF = ($clRF=='txt1')?'txt2':'txt1';	
	$sStyle = '';
	if (date('Y-m-d')>=$acComm['data_iniz']) $sStyle = 'font-weight:bold;';
	if (date('Y-m-d')>$acComm['data_fine']) $sStyle = 'font-style:italic;';
?>
   <tr>
      <td class="<?=$clRF?>" align="center" style='<?=$sStyle?>'>
	      <a href='commView.php?id=<?=$acComm['id_comm']."&$sXSNQS"?>' class="<?=$clRF?>" style='font-family:lucida console,courier;'>
	      	<?=$acComm['id_comm'];?>
	      </a>
      </td>
      <td class="<?=$clRF?>" align="center" style='<?=$sStyle?>'>
	      <a href='commView.php?id=<?=$acComm['id_comm']."&$sXSNQS"?>' class="<?=$clRF?>">
	      	<?=$acComm['cod_comm'];?>
	      </a>
      </td>
      <td class="<?=$clRF?>" align="left" style='<?=$sStyle?>'>
	      <a href='commView.php?id=<?=$acComm['id_comm']."&$sXSNQS"?>' class="<?=$clRF?>">
	      	<?=$acComm['descr'];?>
	      </a>
      </td>
      <td class="<?=$clRF?>" align="center" style='<?=$sStyle?>'><?=$acComm['cod_coge'];?></td>
      <td class="<?=$clRF?>" align="center" style='<?=$sStyle?>'><?=YMD2DMY($acComm['data_iniz']);?></td>
      <td class="<?=$clRF?>" align="center" style='<?=$sStyle?>'><?=YMD2DMY($acComm['data_fine']);?></td>
      <td class="<?=$clRF?>" align="left" style='<?=$sStyle?>'><?=$aUtentiRint[$acComm['id_responsabile']]['nome'];?></td>
      <td class="<?=$clRF?>" align="left" style='<?=$sStyle?>'>
      <? if (isset($aClientiRint[$acComm['id_cliente']])) {
         		echo $aClientiRint[$acComm['id_cliente']]['nome']; 		 
         } else {
         		echo "&nbsp;";	
         } ?>
      </td>      
      <td class="<?=$clRF?>" align="left" style='<?=$sStyle?>'>
      <? if (isset($aCli[$acComm['id_comm']]) && is_array($aCli[$acComm['id_comm']])) {

      	 	$aClienti = $aCli[$acComm['id_comm']];
      	 	sort($aClienti); 
      
			$strCli = "";
			foreach ($aClienti as $nome) {
				$strCli .= $nome.", ";
			}
    		if (count($aClienti) > 0) { 
    			echo substr($strCli, 0, -2);
    		} 
      	 } ?>
      </td>
<? 
} 
?>
</table>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">&nbsp;</td>
      <td class="cmd1" align="right"><?=$sPaginaz?></td>
   </tr>
   <tr>
      <td class="cmd1" align="left"><?=$cmdL?></td>
      <td class="cmd1" align="right"><?=$cmdR?></td>
   </tr>
</table>
<?
echo "</form>";

codaHtml();
exit();
?>

